package com.topisv.tms.utils.print;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.topisv.tms.entity.ExpressStyle;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.entity.work.WorkTask;
import com.topisv.tms.service.ExpressStyleService;
import com.topisv.tms.service.work.WorkOrderService;
import com.topisv.tms.service.work.WorkTaskService;
import com.topisv.tms.utils.ExpressRule;
@Component
public class PickUtil {
	@Autowired
	private WorkOrderService workOrderService;
	@Autowired
	private WorkTaskService workTaskService;
	@Autowired
	private ExpressStyleService expressStyleService;
	/**
	 * 获得要打印的订单号
	 * @param workIds
	 * @param ordeno
	 * @param number
	 * @return
	 * @throws Exception
	 */
	public List<String> getOrdenos(String workIds,String ordeno,int number,StringBuffer sb)throws Exception{
		List<String> ordenos=null;
		List<String> tempOrdenos=null;
		try {
			ordenos=getOrdenos(workIds);
			int size=ordenos.size();
			if("begin".equals(ordeno)){
				number=number>size? size:number;
				tempOrdenos =ordenos.subList(0, number);
			}else{
				int index=ordenos.indexOf(ordeno);
				index+=1;
				number+=index;
				number=number>size? size:number;
				tempOrdenos =ordenos.subList(index, number);
			}
			if(tempOrdenos.get(tempOrdenos.size()-1).equals(ordenos.get(ordenos.size()-1))){
				sb.append("finish");
			}
			return tempOrdenos;
		} catch (Exception e) {
			throw e;
		}
	}
	
	public List<String> getOrdenos(String workIds,String firstExpress,Long expressId)throws Exception{
		List<String> ordenos=new ArrayList<String>();
		try {
			ExpressStyle expressStyle=this.expressStyleService.findOne(expressId);
			String[] works=workIds.split(":");
			int length=works.length;
			for(int i=0;i<length;i++){
				WorkTask workTask=workTaskService.loadTaskById(Long.parseLong(works[i]));
				List<WorkOrder> workOrders=this.workOrderService.findByTaskCode(workTask.getTaskCode());
				int size=workOrders.size();
				for(int j=0;j<size;j++){
					WorkOrder o=workOrders.get(j);
					WorkOrder workOrder=this.workOrderService.loadByExpressNo(firstExpress);
					if(workOrder!=null){
						throw new Exception("快递单号:"+firstExpress+"重复使用");
					}
					if(i==(length-1)&&j==(size-1)){
						ordenos.add(o.getOrder().getOrdeno()+"=="+firstExpress+"=="+o.getRankno()+"==finish");
					}else{
						ordenos.add(o.getOrder().getOrdeno()+"=="+firstExpress+"=="+o.getRankno());
					}
					
					firstExpress=ExpressRule.getExpress(expressStyle.getLogisticsCompany().getCompanyCode(),firstExpress);
				}
			}
			return ordenos;
		} catch (Exception e) {
			throw e;
		}
	}
	
	/**
	 * 根据流水批次id获得订单信息
	 * @param workIds
	 * @return
	 * @throws Exception
	 */
	public List<String> getOrdenos(String workIds)throws Exception{
		List<String> ordenos=new ArrayList<String>();
		try {
			String[] works=workIds.split(":");
			int i=0;
			for(String w:works){
				WorkTask workTask=workTaskService.loadTaskById(Long.parseLong(w));
				List<String> worklinkOrders=getOrdenosString(workTask.getTaskCode());
				ordenos.addAll(worklinkOrders);
				ordenos.add(w);//每个流水批次的订单要分开
				i++;
			}
			return ordenos;
		} catch (Exception e) {
			throw e;
		}
	}
	
	/**
	 * 正常订单分割方式 :流水批次号==订单号==打印序号
	 * @param worklinkOrders
	 * @return
	 * @throws Exception
	 */
	private List<String> getOrdenos(List<WorkOrder> workOrders)throws Exception{
		List<String> ordenos=new ArrayList<String>();
		try {
			//int i=1;
			for(WorkOrder w:workOrders){
				//ordenos.add(w.getOrder().getOrdeno()+"=="+i);
			    //update by LiBin bug595 打印序号问题，当任务中某些订单创建异常时，至使Rankno不连续
			    ordenos.add(w.getOrder().getOrdeno()+"=="+w.getRankno());
				//i++;
			}
			return ordenos;
		} catch (Exception e) {
			throw e;
		}
		
	} 
	
	private List<String> getOrdenosString(String taskCode)throws Exception{
		try {
			List<WorkOrder> workOrders=workOrderService.findByTaskCode(taskCode);
			return getOrdenos(workOrders);
		} catch (Exception e) {
			throw e;
		}
		
	}

}
